class Fibonacci(object):
    def __init__(self, n):
        self.n = n
        self.count = 1
        self.num1 = self.num2 = 1

    def __iter__(self):
        return self

    def __next__(self):
        # 1.
        # if self.index <= self.n:
        #     if self.index == 1:
        #         self.index += 1
        #         return 1
        #     else:
        #         self.index += 1
        #         self.num1, self.num2 = self.num2, self.num1 + self.num2
        #         return self.num1
        # else:
        #     raise StopIteration
        # 2.
        if self.count <= self.n:
            ret = self.num1
            self.count += 1
            self.num1, self.num2 = self.num2, self.num1 + self.num2
            return ret
        else:
            raise StopIteration


f = Fibonacci(12)
for i in f:
    print(i)
print('Fibonacci[12] = {}'.format(i))

# 既然有列表，为什么还要有生成器?
# 要使用1-9999的数据
# nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...]
# range(1, 10000)
# 显然生成器简洁
# 比较：(数据量不大时)
# list：占空间，不占时间
# 生成器：不占空间，占时间
